package com.takeaway.service.serviceimpl;

import com.takeaway.const_.Const;
import com.takeaway.dao.daointerface.BookDao;
import com.takeaway.dao.daointerface.FoodDao;
import com.takeaway.dao.daointerface.ShopDao;
import com.takeaway.dao.daointerface.UserDao;
import com.takeaway.dao.daolmpl.BookDaoImpl;
import com.takeaway.dao.daolmpl.FoodDaoImpl;
import com.takeaway.dao.daolmpl.ShopDaoImpl;
import com.takeaway.dao.daolmpl.UserDaoImpl;
import com.takeaway.pojo.Book;
import com.takeaway.pojo.Food;
import com.takeaway.pojo.Shop;
import com.takeaway.pojo.User;
import com.takeaway.service.serviceinterface.BookService;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 后台赖骏
 */

public class BookServiceImpl implements BookService {
    private static final BookDao BOOK_DAO = new BookDaoImpl();
    private static final UserDao USER_DAO = new UserDaoImpl();
    private static final FoodDao FOOD_DAO = new FoodDaoImpl();
    private static final ShopDao SHOP_DAO = new ShopDaoImpl();
    @Override
    public int insertBook(int userId, int foodId,int dishNum,double couponPrice) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        User user = USER_DAO.selectSingle(new User(), checkField, userId);
        Food food = FOOD_DAO.selectSingle(new Food(), checkField, foodId);
        double remain = user.getMoney()-(food.getPrice()*dishNum- couponPrice);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        remain= Double.parseDouble(decimalFormat.format(remain));
        if (remain<0){
            return 0;
        }
        user.setMoney(remain);
        new UserDaoImpl().update(user);
        return BOOK_DAO.insert(new Book(userId, foodId, 0,dishNum));
    }
    @Override
    public List<Book> checkBook(int userId) throws Exception {
        String[] checkField1 ={Const.FIELD_USERID};
        return BOOK_DAO.selectMore(Book.class, checkField1, userId);
    }

    @Override
    public int deleteBook(int bookId) throws Exception {
        String[] checkField1 ={Const.FIELD_ID};
        return BOOK_DAO.delete(Book.class,checkField1,bookId);
    }

    @Override
    public Object[] checkShopBook(int userId) throws Exception {
        String[] checkField = {Const.FIELD_BUSINESS_ID};
        Shop shop = SHOP_DAO.selectSingle(new Shop(), checkField, userId);
        List<Book> books = BOOK_DAO.selectBookManage(shop.getId());
        List<User> users = USER_DAO.selectBookManage(shop.getId());
        List<Food> foods = FOOD_DAO.selectBookManage(shop.getId());
        Object[] objects = new Object[3];
        objects[0] = books;
        objects[1] = users;
        objects[2] = foods;
        return objects;
    }

    @Override
    public User updateBook(int conditionCode, int bookId,int userId,int foodId) throws Exception {
        String[] checkField={Const.FIELD_ID};
        Book book = BOOK_DAO.selectSingle(new Book(), checkField, bookId);
        User user = USER_DAO.selectSingle(new User(), checkField, userId);
        Food food = new FoodDaoImpl().selectSingle(new Food(), checkField, foodId);
        double remain = user.getMoney()+(food.getPrice()*book.getBookNum());
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        remain= Double.parseDouble(decimalFormat.format(remain));
        user.setMoney(remain);
        USER_DAO.update(user);
        book.setConditionCode(conditionCode);
        BOOK_DAO.update(book);
        return user;
    }

    @Override
    public Object[] checkUserBook(int userId) throws Exception {
        String[] checkField1 ={Const.FIELD_USERID};
        ArrayList<Book> books = BOOK_DAO.selectMore(Book.class, checkField1, userId);
        String[] checkField2 ={Const.FIELD_ID};
        List<Food> foods = new ArrayList<>();
        for (Book book:books) {
            Food food = FOOD_DAO.selectSingle(new Food(), checkField2, book.getFoodId());
            foods.add(food);
        }
        Object[] objects = new Object[2];
        objects[0]=books;
        objects[1]=foods;
        return objects;
    }
}
